<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @row-save="rowSave"
      @on-load="onLoad"
    >
      <template slot-scope="scope" slot="menu">
        <el-button
          icon="el-icon-view"
          size="small"
          type="text"
          @click.stop="handleView(scope.row, scope.index)"
          >查看</el-button
        >
      </template>
      <template slot="menuLeft">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click.stop="handleSave"
          >新增分账接收方</el-button
        >
      </template>
      <template slot-scope="{}" slot="projectCodeSearch">
        <select-page
          ref="searchProjectSelect"
          label="projectName"
          value="projectCode"
          :select.sync="search.projectCode"
          :url="'division/division_project/pageList'"
          placeholder-text="项目"
          filter-key="searchParam"
        ></select-page>
      </template>
      <template slot-scope="{}" slot="rpiNoSearch">
        <select-page
          ref="searchRpiSelect"
          label="rpiName"
          value="rpiNo"
          :select.sync="search.rpiNo"
          :url="website.searchLink.merchantUrl"
          placeholder-text="商户"
          filter-key="searchParam"
        ></select-page>
      </template>
    </avue-crud>

    <!-- 表单弹窗 -->
    <el-dialog
      class="el-dialog__wrapper avue-dialog avue-crud__dialog"
      :title="dialogText"
      :visible.sync="dialogVisible"
      append-to-body
      :close-on-click-modal="false"
      :before-close="handleClose"
      width="60%"
    >
      <el-form
        ref="form"
        :model="receiveForm"
        :rules="rules"
        label-width="120px"
      >
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectCode">
              <select-page
                ref="searchProjectSelect"
                label="projectName"
                value="projectCode"
                :select.sync="receiveForm.projectCode"
                :url="'division/division_project/pageList'"
                placeholder-text="项目"
                filter-key="searchParam"
                @change="projectChange"
              ></select-page>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="商户名称" prop="rpiNo">
              <select-page
                ref="searchRpiSelect"
                label="rpiName"
                value="rpiNo"
                :select.sync="receiveForm.rpiNo"
                :url="website.searchLink.merchantUrl"
                placeholder-text="商户"
                filter-key="searchParam"
                :params="rpiParams"
              ></select-page>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="授权证明" prop="fileAuthCert">
              <el-upload
                action=""
                list-type="picture-card"
                :http-request="handleAuthCertRequest"
                :limit="1"
                :on-remove="authCertRemove"
                :before-remove="beforeauthCertRemove"
                :file-list="authCertFileList"
                :before-upload="beforeUpload"
                :on-preview="imgPreview"
              >
                <i slot="default" class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分账协议" prop="fileAgreement">
              <el-upload
                action=""
                list-type="picture-card"
                :http-request="handleAgreementRequest"
                :limit="1"
                :on-remove="agreementRemove"
                :before-remove="beforeagreementRemove"
                :file-list="agreementFileList"
                :before-upload="beforeUpload"
                :on-preview="imgPreview"
              >
                <i slot="default" class="el-icon-plus"></i>
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="avue-dialog__footer avue-dialog__footer--right">
        <el-button size="small" type="primary" v-preventClick @click="handleSubmit">确 定</el-button>
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <el-image-viewer
      style="z-index: 99999;"
      v-if="imgShow"
      :on-close="()=>{ imgShow = false }"
      :url-list="imgUrlList"
    ></el-image-viewer>
  </basic-container>
</template>

<script>
import { getList, add } from "@/api/rb-oem/separateReceive/separate_receive";
import { mapGetters } from "vuex";
import website from "@/config/website";
import selectPage from "@/components/select-page/select-page";
import { uploadPrivate } from "@/api/system/oss";

export default {
  name: "SeparateReceive",
  components: {
    selectPage
  },
  data() {
    return {
      website,
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      authCertFileList: [],
      agreementFileList: [],
      option: {
        height: "calc(100vh - 200px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: true,
        searchMenuSpan: 24,
        columnBtn: false,
        labelWidth: 120,
        searchLabelWidth: 120,
        menuWidth: 150,
        column: [
          {
            label: "项目编号",
            searchLabel: "项目名称",
            prop: "projectCode",
            search: true,
          },
          {
            label: "项目名称",
            prop: "projectName",
          },
          {
            label: "分账接收方编号",
            searchLabel: "分账接收方名称",
            prop: "rpiNo",
            search: true,
          },
          {
            label: "分账接收方名称",
            prop: "rpiName",
          },
          // {
          //   label: "授权证明",
          //   prop: "fileAuthCertUrl",
          //   type: "upload",
          //   listType: "picture-img",
          //   width: 120
          // },
          // {
          //   label: "分账协议",
          //   prop: "fileAgreementUrl",
          //   type: "upload",
          //   listType: "picture-img",
          //   width: 120
          // },
          {
            label: "创建日期",
            prop: "createTime",
          }
        ]
      },
      data: [],
      dialogVisible: false,
      dialogType: "",
      dialogText: "新增收款方",
      receiveForm: {
        projectCode: "",
        projectName: "",
        rpiNo: "",
        rpiName: "",
        fileAuthCert: "",
        fileAgreement: ""
      },
      rules: {
        projectCode: [
          { required: true, message: "请选择项目", trigger: "change" }
        ],
        rpiNo: [
          { required: true, message: "请选择商户", trigger: "change" }
        ],
        fileAuthCert: [
          { required: true, message: "请上传授权证明", trigger: "change" }
        ],
        fileAgreement: [
          { required: true, message: "请上传分账协议", trigger: "change" }
        ]
      },
      rpiParams: {
        aisleCode: ''
      },
      imgShow:false,
      imgUrlList:[],
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.separate_receive_add, false),
        viewBtn: this.vaildData(this.permission.separate_receive_view, false),
        editBtn: this.vaildData(this.permission.separate_receive_edit, false)
      };
    }
  },
  methods: {
    searchReset() {
      this.query = {};
      this.search = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.onLoad(this.page);
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.onLoad(this.page);
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    sortChange(val) {
      if (val.order == "ascending") {
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
    handleSave() {
      this.dialogVisible = true;
      this.dialogType = "add";
      this.$nextTick(() => {
        this.$refs.form.resetFields();
        this.authCertFileList = [];
        this.agreementFileList = [];
      });
    },
    handleView(row) {
      this.$router.push({
        path: "/rb-oem/posinfo/pos_info_reg_details",
        query: {
          d: this.$crypto.encrypt(JSON.stringify({
            rpiNo: row.rpiNo,
          }))
        }
      });
    },
    beforeUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2;
      if (!isLt2M) {
        this.$message.error('上传文件大小不能超过 2MB!');
      }
      return isLt2M;
    },
    /* 预览图片 */
    imgPreview(file) {
        this.imgUrlList[0] = file.url;
        this.imgShow = true;
    },
    /* 上传 */
    handleAuthCertRequest(option) {
      uploadPrivate(option.file).then((res) => {
        this.receiveForm.fileAuthCert = res.data.data.name;
        this.$refs.form.validateField("fileAuthCert");
      });
    },
      /* 删除 */
      authCertRemove(file, fileList) {
      this.authCertFileList = fileList;
      this.receiveForm.fileAuthCert = "";
    },
    beforeauthCertRemove() {
      return this.$confirm(`确定移除分账委托授权书？`);
    },
    handleAgreementRequest(option) {
      uploadPrivate(option.file).then((res) => {
        this.receiveForm.fileAgreement = res.data.data.name;
        this.$refs.form.validateField("fileAgreement");
      });
    },
    agreementRemove(file, fileList) {
      this.agreementFileList = fileList;
      this.receiveForm.fileAgreement = "";
    },
    beforeagreementRemove() {
      return this.$confirm(`确定移除商户分账协议？`);
    },
    projectChange(e, row) {
      if(e){
        this.rpiParams.aisleCode = row.aisleCode
        this.receiveForm.rpiNo = ''
        this.$refs.searchRpiSelect.getOptionList(this.rpiParams)
      }else{
        this.rpiParams.aisleCode = ''
        this.$refs.searchRpiSelect.getOptionList({})
      }
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const loading = this.$loading({
            lock: true,
            text: "提交中...",
            spinner: "el-icon-loading"
          });
          add(this.receiveForm)
            .then(() => {
              this.$message.success("操作成功");
              this.dialogVisible = false;
              this.refreshChange();
            })
            .finally(() => {
              loading.close();
            });
        }
      });
    },
    handleClose(done) {
      this.$refs.form.resetFields();
      this.authCertFileList = [];
      this.agreementFileList = [];
      done();
    }
  }
};
</script>

<style lang="scss" scoped>
.separate-receive {
  /deep/ .el-upload-list--picture-card .el-upload-list__item {
    width: 100px;
    height: 100px;
  }
  /deep/ .el-upload--picture-card {
    width: 100px;
    height: 100px;
    line-height: 100px;
  }
}
</style>